<template>
  <!--辅计量单位-->
  <div v-if="editDialogVisible12">
    <!--按钮和列表显示-->
    <el-form label-width="100px" :inline="true" :size="size">
      <el-row class="tr">
        <el-form-item>
        <kt-button icon="fa fa-plus" :label="$t('action.add')" type="primary" @click="handleUnitsAdd"/>
      </el-form-item>
      </el-row>

      <el-table :data="productUnitList" border tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="name" label="计量单位"></el-table-column>
        <el-table-column prop="code" label="单位编码"></el-table-column>
        <el-table-column prop="conversionRate" label="转换率"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button :size="size" type="primary" @click="handleUnitsEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>

    <!--添加辅计量单位弹窗-->
    <el-dialog title="添加辅计量单位" width="50%" :visible.sync="productUnitDialogAdd" :close-on-click-modal="false">
      <el-form :model="productUnitDialogAddForm" label-width="30%" :rules="productUnitDialogAddRules"
               ref="productUnitDialogAddForm" :size="size">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="productUnitDialogAddForm.id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="辅计量单位" prop="units">
          <el-select v-model="productUnitDialogAddForm.units" style="width:60%">
            <el-option
              v-for="item in productUnitDialogAddFormList"
              :key="item.id"
              :label="item.code +'-'+ item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主计量单位">
          <label>{{unitsName}}</label>
        </el-form-item>
        <el-form-item label="转换率(辅对主)" prop="conversionRate">
          <el-input v-model="productUnitDialogAddForm.conversionRate" auto-complete="off" style="width:60%"></el-input>
          <el-tooltip
            class="item"
            effect="dark"
            content="举例: 比如主计量单位是袋, 辅计量单位是克, 一袋对应100克, 那么转换率(辅对主)填写0.01"
            placement="top-start">
            <el-button
              size="small"
              icon="el-icon-question"
              circle></el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="productUnitDialogAdd = false">{{$t("action.cancel") }}
        </el-button>
        <el-button :size="size" type="primary" @click.native="productUnitSubmitAddForm">{{ $t("action.submit") }}
        </el-button>
      </div>
    </el-dialog>

    <!--修改辅计量单位弹窗-->
    <el-dialog title="修改辅计量单位" width="50%" :visible.sync="productUnitDialogUpdate" :close-on-click-modal="false">
      <el-form :model="productUnitDialogUpdateForm" label-width="30%" :rules="productUnitDialogUpdateRules"
               ref="productUnitDialogUpdateForm" :size="size">
        <el-form-item label="ID：" prop="id" v-if="false">
          <el-input v-model="productUnitDialogUpdateForm.id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="辅计量单位：" prop="units">
          <el-select v-model="productUnitDialogUpdateForm.units" style="width:60%" :disabled="true">
          </el-select>
        </el-form-item>
        <el-form-item label="主计量单位：">
          <label>{{unitsName}}</label>
        </el-form-item>
        <el-form-item label="转换率(辅对主)" prop="conversionRate">
          <el-input v-model="productUnitDialogUpdateForm.conversionRate" auto-complete="off"
                    style="width:60%" placeholder="请输入转换率(辅对主)"></el-input>
          <el-tooltip class="item" effect="dark"
                      content="举例: 比如主计量单位是袋, 辅计量单位是克, 一袋对应100克, 那么转换率(辅对主)填写0.01" placement="top-start">
            <el-button size="small" icon="el-icon-question" circle></el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="productUnitDialogUpdate = false">{{$t("action.cancel") }}
        </el-button>
        <el-button :size="size" type="primary" @click.native="productUnitSubmitUpdateForm">{{ $t("action.submit") }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import CyTable from "@/views/Core/CyTable";
  import KtButton from "@/views/Core/KtButton";

  export default {
    components: {
      KtButton,
      CyTable,
    },

    data() {
      return {
        size: "small",
        editDialogVisible12: false,
        editDialogVisible83: false,
        editDialogVisible84: false,
        editLoading: false,
        productId: 0,
        getProductParentList: [],
        getProductParentAttrList: [],
        sonProductAttrData: [],
        categoryPropertyCheckList: [],
        sonProductAttrDataList: [],
        categoryId: 0,
        productUnitList: [],
        productUnitDialogAdd: false, // 计量单位是否显示
        productUnitDialogAddForm: {
          id: '',
          units: '',
          conversionRate: ''
        },
        productUnitDialogAddFormList: [], // 计量单位 辅计量单位下拉框
        unitsProductId: '', // 系列子品计量单位和计量单位的公用productId
        codeByShopDataForm: {
          storeId: '',
          freightTemplateId: ''
        },
        productUnitDialogAddRules: {
          units: [
            {required: true, message: "请输入辅计量单位", trigger: "blur"}
          ],
          conversionRate: [
            {required: true, message: "请输入转换率(辅对主)", trigger: "blur"}
          ]
        },
        productUnitDialogUpdateRules: {
          units: [
            {required: true, message: "请输入辅计量单位", trigger: "blur"}
          ],
          conversionRate: [
            {required: true, message: "请输入转换率(辅对主)", trigger: "blur"}
          ]
        },
        productUnitDialogUpdate: false, // 计量单位是否显示
        productUnitDialogUpdateForm: {
          id: '',
          units: '',
          conversionRate: ''
        },
        unitsName: '',
      };
    },

    methods: {
      init(param) {
        this.editDialogVisible12 = param.flag;
        this.productId = param.productId;
        this.unitsName = param.unitsName;
        if (param.flag) {
          this.handleUnitsFind()
        }
      },

      // 计量单位列表查询
      handleUnitsFind() {
        let this_ = this;
        this.utils.request.queryProductUnitsRelation({productId: this_.productId}, function (res) {
          this_.productUnitList = res.data;
        });
      },

      // 计量单位添加弹窗
      handleUnitsAdd() {
        let this_ = this;
        this.utils.request.queryProductUnitsList(null, function (res) {
          this_.productUnitDialogAddFormList = res.data;
        });
        if (this_.$refs["productUnitDialogAddForm"]) {
          this_.$refs["productUnitDialogAddForm"].resetFields();
        }
        this_.productUnitDialogAdd = true;
      },

      // 计量单位添加弹窗提交
      productUnitSubmitAddForm() {
        let this_ = this;
        this.$refs.productUnitDialogAddForm.validate(valid => {
          if (valid) {
            let params = {};
            params.productId = this_.productId;
            params.productUnitsId = this_.productUnitDialogAddForm.units;
            params.conversionRate = this_.productUnitDialogAddForm.conversionRate;
            this.utils.request.addProductUnitsRelation(params, function (res) {
              if (res.code == "0000") {
                this_.$message({message: "操作成功", type: "success"});
                this_.handleUnitsFind();
                this_.productUnitDialogAdd = false
              } else {
                this_.$message({message: "操作失败," + res.msg, type: "error"});
              }
            });
          }
        });
      },

      // 计量单位修改弹窗
      handleUnitsEdit(params) {
        this.productUnitDialogUpdate = true;
        this.productUnitDialogUpdateForm.id = params.id;
        this.productUnitDialogUpdateForm.units = params.name + '-' + params.code;
        this.productUnitDialogUpdateForm.conversionRate = params.conversionRate;
      },

      // 计量单位修改弹窗提交
      productUnitSubmitUpdateForm() {
        let this_ = this;
        this.$refs.productUnitDialogUpdateForm.validate(valid => {
          if (valid) {
            let params = {};
            params.id = this_.productUnitDialogUpdateForm.id;
            params.conversionRate = this_.productUnitDialogUpdateForm.conversionRate;
            this.utils.request.updateProductUnitsRelation(params, function (res) {
              if (res.code == "0000") {
                this_.$message({message: "操作成功", type: "success"});
                this_.handleUnitsFind();
                this_.productUnitDialogUpdate = false
              } else {
                this_.$message({message: "操作失败," + res.msg, type: "error"});
              }
            });
          }
        });
      },
    }

  }
</script>

<style scoped lang='scss'>
.tr{
  text-align: right;
  .el-form-item{
  margin-bottom: 18px;
 }
}
.el-form-item{
  margin-bottom: 30px;
}
</style>
